package com.starmark.gateway.auth.provider.web;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.starmark.core.framework.base.AbstractController;
import com.starmark.core.framework.util.BeanMapper;
import com.starmark.gateway.auth.provider.entity.GatewayAuthRole;
import com.starmark.gateway.auth.provider.service.IGatewayAuthRoleService;
import com.starmark.gateway.auth.provider.vo.AuthOrgElementDto;
import com.starmark.gateway.auth.provider.vo.GatewayAuthResourceVo;
import com.starmark.gateway.auth.provider.vo.GatewayAuthRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author starmark
 * @since 2018-04-27
 */
@RestController
@Api(value = "系统权限角色", description = "系统权限角色")
@RequestMapping("/gateway/auth/role")
public class GatewayAuthRoleController extends AbstractController {
    @Autowired
    private IGatewayAuthRoleService sysAuthRoleService;


    @ApiOperation(value = "查询角色列表")
    @PostMapping(value = "/query")
    public Object query(@RequestBody Map<String, Object> param) {

        return BeanMapper.mapPage(sysAuthRoleService.query(param), GatewayAuthRoleVo.class);
    }

    @PostMapping
    @ApiOperation(value = "新增角色")
    public GatewayAuthResourceVo save(@RequestBody GatewayAuthRoleVo gatewayAuthRoleVo) {
        GatewayAuthRole gatewayAuthRole = BeanMapper.map(gatewayAuthRoleVo, GatewayAuthRole.class);
        sysAuthRoleService.save(gatewayAuthRole);
        return BeanMapper.map(gatewayAuthRole, GatewayAuthResourceVo.class);
    }

    @ApiOperation(value = "角色详情")
    @GetMapping(value = "/{id}")
    public GatewayAuthResourceVo get(@PathVariable("id") String id) {
        GatewayAuthRole gatewayAuthRole = sysAuthRoleService.getById(id);
        return BeanMapper.map(gatewayAuthRole, GatewayAuthResourceVo.class);
    }

    @PutMapping
    @ApiOperation(value = "修改角色")
    public GatewayAuthResourceVo update(@RequestBody GatewayAuthRoleVo gatewayAuthRoleVo) {
        GatewayAuthRole gatewayAuthRole = BeanMapper.map(gatewayAuthRoleVo, GatewayAuthRole.class);
        sysAuthRoleService.updateById(gatewayAuthRole);
        return BeanMapper.map(gatewayAuthRole, GatewayAuthResourceVo.class);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "删除角色")
    public void delete(@PathVariable("id") String id) {

        sysAuthRoleService.removeById(id);

    }

    @GetMapping(value = "/resource/{id}")
    @ApiOperation(value = "获取角色关联的资源Id")
    public Object getRoleResource(@PathVariable("id") String roleId) {
        return sysAuthRoleService.getRoleResource(roleId);
    }


    @PutMapping(value = "/resource/{id}")
    @ApiOperation(value = "保存角色关联的资源Id")
    public void saveRoleResource(@PathVariable("id") String roleId, @RequestBody JSONObject jsonObject) {
        JSONArray ids = jsonObject.getJSONArray("id");
        List<String> resourceIds = ids.parallelStream().map(Object::toString).collect(Collectors.toList());

        sysAuthRoleService.grantAuthToRole(roleId, resourceIds);

    }





    @GetMapping(value = "/org/{id}")
    @ApiOperation(value = "获取角色关联的组织")
    public Object getRoleOrg(@PathVariable("id") String roleId) {
        return sysAuthRoleService.getRoleOrg(roleId);
    }


    @PutMapping(value = "/org/{id}")
    @ApiOperation(value = "保存角色关联的组织")
    public void saveRoleOrg(@PathVariable("id") String roleId, @RequestBody List<AuthOrgElementDto> orgElementDto) {

        sysAuthRoleService.grantAuthOrgRole(roleId, orgElementDto);

    }



}
